﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMovie.Models;

namespace MvcMovie.Controllers {
    public class MoviesController : Controller {
        private MovieDbContext db = new MovieDbContext();

        // GET: /Movies/
        public ActionResult Index() {
            return View(db.Movies.ToList());
        }

        // GET: /Movies/Details/5
        public ActionResult Details(int id = 0) {
            Movie movie = db.Movies.Find(id);

            if (movie == null) {
                return HttpNotFound();
            }
            
            return View(movie);
        }

        // GET: /Movies/Create
        public ActionResult Create() {
            return View();
        }

        // POST: /Movies/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Movie movie) {
            
            if (ModelState.IsValid) {
                db.Movies.Add(movie);
                db.SaveChanges();
                
                return RedirectToAction("Index");
            }

            return View(movie);
        }

        // GET: /Movies/Edit/5
        public ActionResult Edit(int id = 0) {
            Movie movie = db.Movies.Find(id);
            
            if (movie == null) {
                return HttpNotFound();
            }
            
            return View(movie);
        }

        // POST: /Movies/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Movie movie) {
            
            if (ModelState.IsValid) {
                db.Entry(movie).State = EntityState.Modified;
                db.SaveChanges();
                
                return RedirectToAction("Index");
            }

            return View(movie);
        }

        // GET: /Movies/Delete/5
        public ActionResult Delete(int id = 0) {
            Movie movie = db.Movies.Find(id);
            
            if (movie == null) {
                return HttpNotFound();
            }
            
            return View(movie);
        }

        // POST: /Movies/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id) {
            Movie movie = db.Movies.Find(id);
            
            db.Movies.Remove(movie);
            db.SaveChanges();
            
            return RedirectToAction("Index");
        }

        // GET: /Movies/Search
        public ActionResult Search(string movieGenre, string searchString) {
            var genreList = new List<string>();
            var genreQuery = from m in db.Movies
                             orderby m.Genre
                             select m.Genre;
            genreList.AddRange(genreQuery.Distinct());
            ViewBag.MovieGenre = new SelectList(genreList);
            var movies = from m in db.Movies
                         select m;

            if (!string.IsNullOrEmpty(searchString)) {
                movies = movies.Where(t => t.Title.Contains(searchString));
            }

            if (!string.IsNullOrEmpty(movieGenre)) {
                movies = movies.Where(m => m.Genre == movieGenre);
            }

            return View(movies);
        }

        protected override void Dispose(bool disposing) {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}